﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using Minecraft.Model.Treasurebox;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class MapPrefabSourceSelect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Map_MapPrefabSourceSelect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<MapPrefabSourceSelectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}

			if (req.mapId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mapId 参数为空）" });
				return;
			}
			//var csvMapInfo = CsvConfig.mapList.FirstOrDefault(m => m.Id == req.mapId);
			var csvMapInfo = CsvConfig.GetMod_MapInfoById(req.mapId);
			if (csvMapInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：csvMapInfo 配置信息为空【req.mapId：{req.mapId}】）" });
				return;
			}

			//预制宝箱列表加载
			var treasureBoxPrefabList = session.minecraftSessionInfo.furnitureTreasureBoxList.Where(m => m.enumFurnitureType == EnumFurnitureType.Treasurebox && m.TreasureBox_MapId == req.mapId && m.TreasureBox_IsPrefab_Convert == true).ToList();
			var treasureBoxPrefabInfoList = treasureBoxPrefabList.Select(m => new TreasureBoxPrefabInfo()
			{
				treasureboxId = m.FurnitureId,
				itemId = m.FurnitureItemId,
				point3 = m.point3,
				angle = m.angle
			}).ToList();

			var resp = new MapPrefabSourceSelectResp
			{
				mapId = req.mapId,
				treasureBoxPrefabInfoList = treasureBoxPrefabInfoList
			};
			session.Send(defCommand, req, resp);
		}
	}
}
